//外观数列
//测试链接 https://leetcode.cn/problems/count-and-say/
public class CountAndSay {
    public String countAndSay(int n) {
        String ret = "1";

        for(int i = 1; i <n; i++){
            ret = exchange(ret);
        }

        return ret;
    }

    public String exchange(String s){
        int n = s.length();
        StringBuilder sb = new StringBuilder();

        for(int left = 0, right = 0; right < n; ){
            while(right < n && s.charAt(left) == s.charAt(right)){
                right++;
            }
            int count = right - left;
            sb.append(""+count);
            sb.append(""+s.charAt(left));
            left = right;
        }

        return sb.toString();
    }
}
